<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	小程序粉丝管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['isread']=array('0'=>'未读','1'=>'已读');
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$chatid=intval($_RQ['id']);
		$params['chatid']=$chatid;
	}
	if($_RQ['nickname']){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$list=pdo_getslice('jy_kefu_chat',$params,array($pindex,$psize),$total,$fields);
	if($list){
		foreach($list as &$row){
			$memberchat = pdo_get('jy_kefu_member_chat',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$row['id']));
			$getmember = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$memberchat['getmid']),array('nickname','avatar'));
			$setmember = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$memberchat['mid']),array('nickname','avatar'));
			$row['avatar']=$setmember['avatar'];
			$row['nickname']=$setmember['nickname'];
			$row['getavatar']=$getmember['avatar'];
			$row['getname']=$getmember['nickname'];
			if($memberchat['msgtype'] ==1 ){
				$row['lastmsg']=$memberchat['lastmsg'];
			}elseif($memberchat['msgtype'] ==3 ){
				$row['lastmsg']='语音信息';
			}elseif($memberchat['msgtype'] ==4 ){
				$row['lastmsg']='图片信息';
			}
			$row['lasttime']=date('20y/m/d H:i:s',$memberchat['lasttime']);
			if(empty($memberchat['mid'])){
				unset($row);
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'聊天消息'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
				array('name'=>'avatar','title'=>'头像','type'=>'image'),
				array('name'=>'nickname','title'=>'发起昵称','type'=>'string'),
				array('name'=>'getavatar','title'=>'对方头像','type'=>'image'),
				array('name'=>'getname','title'=>'对方昵称','type'=>'string'),
				array('name'=>'lastmsg','title'=>'最近消息','type'=>'string'),
				array('name'=>'lasttime','title'=>'时间','type'=>'string'),
			),'action'=>array(
				array('type'=>'href','op'=>'detail','title'=>'详情'),
			)
		),
		'search_one'=>array('name'=>'nickname','add'=>false)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if($id){
		$list=pdo_getall('jy_kefu_chatlog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$id));
	}else{
		exi('没获取到会话！','error');
	}
	if($list){
		foreach($list as &$row){
			$getmember = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['getmid']),array('nickname','avatar'));
			$setmember = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['setmid']),array('nickname','avatar'));
			$row['avatar']=$setmember['avatar'];
			$row['nickname']=$setmember['nickname'];
			$row['getavatar']=$getmember['avatar'];
			$row['getname']=$getmember['nickname'];
			if($row['type'] ==3 ){
				$row['msg']='语音信息';
			}elseif($row['type'] ==4 ){
				$row['msg']='图片信息';
			}
			$row['createtime']=date('20y/m/d H:i:s',$row['createtime']);
		}
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'聊天列表'),
		),
		'list'=>array('data'=>'list','del_op'=>'del2','edit'=>false,'list'=>array(
				array('name'=>'avatar','title'=>'头像','type'=>'image'),
				array('name'=>'nickname','title'=>'发起昵称','type'=>'string'),
				array('name'=>'getavatar','title'=>'对方头像','type'=>'image'),
				array('name'=>'getname','title'=>'对方昵称','type'=>'string'),
				array('name'=>'msg','title'=>'消息内容','type'=>'string'),
				array('name'=>'createtime','title'=>'时间','type'=>'string'),
			)
		),
		'search_one'=>array('name'=>'nickname','add'=>false)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kefu_chat',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='del2'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kefu_chatlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
?>